﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common;
using System.Collections;
using System.Configuration;
using System.Reflection;
using DTC.Models;
using System.Threading;

namespace DTC
{
    public partial class FrmDisplay : UIBase
    {
        FIO01 m_io;
        Hashtable htStateImage = new Hashtable();
        ProductMachine _cellLIne;//机器的抽象表示
        public string CellName { get; set; }
        public string ModelName { get; set; }
        System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer(); //用于检测IO状态
        #region 报表属性集合
        public string SummaryPassCount
        {
            get
            {
                return listView1.Items[0].SubItems[1].Text;
            }
            set
            {
                listView1.Items[0].SubItems[1].Text = value;
            }
        }
        public string SummaryPassRate
        {
            get
            {
                return listView1.Items[0].SubItems[2].Text;
            }
            set
            {
                listView1.Items[0].SubItems[2].Text = value;
            }
        }
        public string SummaryFailCount
        {
            get
            {
                return listView1.Items[1].SubItems[1].Text;
            }
            set
            {
                listView1.Items[1].SubItems[1].Text = value;
            }
        }
        public string SummaryFailRate
        {
            get
            {
                return listView1.Items[1].SubItems[2].Text;
            }
            set
            {
                listView1.Items[1].SubItems[2].Text = value;
            }
        }
        public string SummaryTotalQty
        {
            get
            {
                return listView1.Items[2].SubItems[1].Text;
            }
            set
            {
                listView1.Items[2].SubItems[1].Text = value;
            }
        }
        #region marked
        //public string ny1PassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[5].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[5].SubItems[1].Text = value;
        //    }
        //}
        //public string ny1FailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[5].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[5].SubItems[2].Text = value;
        //    }
        //}
        //public string ny1TotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[5].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[5].SubItems[3].Text = value;
        //    }
        //}
        //public string ny1FailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[5].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[5].SubItems[4].Text = value;
        //    }
        //}
        //public string ny2PassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[6].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[6].SubItems[1].Text = value;
        //    }
        //}
        //public string ny2FailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[6].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[6].SubItems[2].Text = value;
        //    }
        //}
        //public string ny2TotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[6].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[6].SubItems[3].Text = value;
        //    }
        //}
        //public string ny2FailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[6].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[6].SubItems[4].Text = value;
        //    }
        //}
        //public string ny3PassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[7].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[7].SubItems[1].Text = value;
        //    }
        //}
        //public string ny3FailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[7].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[7].SubItems[2].Text = value;
        //    }
        //}
        //public string ny3TotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[7].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[7].SubItems[3].Text = value;
        //    }
        //}
        //public string ny3FailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[7].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[7].SubItems[4].Text = value;
        //    }
        //}
        //public string lg1PassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[8].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[8].SubItems[1].Text = value;
        //    }
        //}
        //public string lg1FailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[8].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[8].SubItems[2].Text = value;
        //    }
        //}
        //public string lg1TotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[8].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[8].SubItems[3].Text = value;
        //    }
        //}
        //public string lg1FailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[8].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[8].SubItems[4].Text = value;
        //    }
        //}
        //public string lg2PassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[9].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[9].SubItems[1].Text = value;
        //    }
        //}
        //public string lg2FailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[9].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[9].SubItems[2].Text = value;
        //    }
        //}
        //public string lg2TotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[9].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[9].SubItems[3].Text = value;
        //    }
        //}
        //public string lg2FailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[9].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[9].SubItems[4].Text = value;
        //    }
        //}
        //public string zcPassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[10].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[10].SubItems[1].Text = value;
        //    }
        //}
        //public string zcFailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[10].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[10].SubItems[2].Text = value;
        //    }
        //}
        //public string zcTotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[10].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[10].SubItems[3].Text = value;
        //    }
        //}
        //public string zcFailRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[10].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[10].SubItems[4].Text = value;
        //    }
        //}
        //public string txPassCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[11].SubItems[1].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[11].SubItems[1].Text = value;
        //    }
        //}
        //public string txFailCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[11].SubItems[2].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[11].SubItems[2].Text = value;
        //    }
        //}
        //public string txTotalCount
        //{
        //    get
        //    {
        //        return listViewEx1.Items[11].SubItems[3].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[11].SubItems[3].Text = value;
        //    }
        //}
        //public string txPassRate
        //{
        //    get
        //    {
        //        return listViewEx1.Items[11].SubItems[4].Text;
        //    }
        //    set
        //    {
        //        listViewEx1.Items[11].SubItems[4].Text = value;
        //    }
        //}
        #endregion
        #endregion


        public FrmDisplay()
        {
            InitializeComponent();
        }

        private void FrmDisplay_Load(object sender, EventArgs e)
        {

            _cellLIne = new ProductMachine(CellName);//备注：需要Cellname判断是不是存在或为空
            _cellLIne.ShowMsg = ShowMsg;  
            _cellLIne.className = "TMC01";
            _cellLIne.ModelName = ModelName;
            IOCardInitial();
            _cellLIne.m_IO = m_io;
            _cellLIne.ShowTestSpan = ShowTestSpan;
            LoadStateImage();

            this.listView2.Items[1].SubItems[1].Text = System.Net.Dns.GetHostName();
            this.listView2.Items[2].SubItems[1].Text = "";
            this.listView2.Items[0].SubItems[1].Text = ModelName;
            tmr.Interval = 100;
            tmr.Tick += tmr_Tick;
            tmr.Enabled = false;
        }

        private void ShowTestSpan(List<TestSpan> obj)
        {
            if (InvokeRequired)
            {
                this.BeginInvoke(new Action<List<TestSpan>>(ShowTestSpan), new object[] { obj });
            }
            else
            {
                this.dataGridView1.DataSource = new BindingList<TestSpan>(new List<TestSpan>(obj));
                Changedgvx1Corlor();
            }
        }

        private void IOCardInitial()
        {
            try
            {
                m_io = new FIO01();
                m_io.OpenIOCard();
                m_io.b0EdgeEvent += StartTestProc;
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "开启IO卡成功");
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, m_io.State.ToString());
            }
            catch (Exception ex)
            {
                ShowError(ex.Message + Environment.NewLine
                    + "IO Card 打开失败，请确认驱动安装和IO Card连接状态。");
            }
        }

        private void tmr_Tick(object sender, EventArgs e)
        {
            try
            {
                IOState io = m_io.State;
                buttonX1.Checked = io.io0;
                buttonX2.Checked = io.io1;
                buttonX3.Checked = io.io2;
                buttonX4.Checked = io.io3;
                buttonX5.Checked = io.io4;
                buttonX6.Checked = io.io5;
                buttonX7.Checked = io.io6;
                buttonX8.Checked = io.io7;
                buttonX9.Checked = io.io8;
                buttonX10.Checked = io.io9;
                buttonX11.Checked = io.io10;
                buttonX12.Checked = io.io11;
                buttonX13.Checked = io.io12;
                buttonX14.Checked = io.io13;
                buttonX15.Checked = io.io14;
                buttonX16.Checked = io.io15;
                buttonX17.Checked = m_io.CurrentDO[0];
                buttonX18.Checked = m_io.CurrentDO[1];
            }
            catch (Exception ex)
            {
        //        ShowError(ex.Message);
                LogHelper.WriteLog(typeof(FrmDisplay), ex);
                tmr.Enabled = false;
            }
        }

        private void LoadStateImage()
        {
            try
            {
                Image imgRun = Image.FromFile(@".\Pic\RUN.jpg");
                htStateImage.Add("RUN", imgRun);
                Image imgRead = Image.FromFile(@".\Pic\read.jpg");
                htStateImage.Add("READ", imgRead);
                Image imgStandby = Image.FromFile(@".\Pic\STANDBY.jpg");
                htStateImage.Add("STANDBY", imgStandby);
                Image imgStop = Image.FromFile(@".\Pic\STOP.jpg");
                htStateImage.Add("STOP", imgStop);
                Image imgFinishing = Image.FromFile(@".\Pic\Finishing.jpg");
                htStateImage.Add("FINISHING", imgFinishing);

            }
            catch (Exception ex)
            {
                ShowError(ex.Message);
            }
        }

        private void StartTestProc()
        {

            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(StartTestProc));
                return;
            }
            Thread trd = new Thread(testMain);
            trd.IsBackground = true;
            trd.Start();
            this.listBox1.Items.Clear();
        }
        private void testMain()
        {
            try
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "————————测试事件触发————————");
                _cellLIne.Step();
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "————————测试事件结束————————");

            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(typeof(FrmDisplay), ex);
             //   ShowError(ex.Message);
            }
        }

        public void Start()
        {
            pbState.Image = (Image)htStateImage["RUN"];
            tmr.Enabled = true;

        }
        public void Stop()
        {
            pbState.Image = (Image)htStateImage["STOP"];
        }
        public void Finishing()
        {
            pbState.Image = (Image)htStateImage["FINISHING"];
        }

        public void ReSetState()
        {
            ResetState();
            pbState.Image = (Image)htStateImage["STANDBY"];
        }

        private void ShowMsg(string msg)
        {
            if (InvokeRequired)
            {
                this.Invoke(new Action<string>(ShowMsg), new object[] { msg });
                return;
            }
            listBox1.Items.Add(msg);
           LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, msg);
            if (msg.Contains("结果"))
            {
                switch (msg)
                {
                    #region marked

                    //case "[\"耐压1\"结果]:成功":
                    //    this.ny1PassCount = (int.Parse(ny1PassCount) + 1).ToString();
                    //    break;
                    //case "[\"耐压1\"结果]:失败":
                    //    this.ny1FailCount = (int.Parse(ny1FailCount) + 1).ToString();
                    //    break;
                    //case "[\"耐压2\"结果]:成功":
                    //    this.ny2PassCount = (int.Parse(ny2PassCount) + 1).ToString();
                    //    break;
                    //case "[\"耐压2\"结果]:失败":
                    //    this.ny2FailCount = (int.Parse(ny2FailCount) + 1).ToString();
                    //    break;
                    //case "[\"耐压3\"结果]:成功":
                    //    this.ny3PassCount = (int.Parse(ny3PassCount) + 1).ToString();
                    //    break;
                    //case "[\"耐压3\"结果]:失败":
                    //    this.ny3FailCount = (int.Parse(ny3FailCount) + 1).ToString();
                    //    break;
                    //case "[\"漏感1\"结果]:失败":
                    //    this.lg1FailCount = (int.Parse(lg1FailCount) + 1).ToString();
                    //    break;
                    //case "[\"漏感1\"结果]:成功":
                    //    this.lg1PassCount = (int.Parse(lg1PassCount) + 1).ToString();
                    //    break;
                    //case "[\"漏感2\"结果]:失败":
                    //    this.lg2FailCount = (int.Parse(lg2FailCount) + 1).ToString();
                    //    break;
                    //case "[\"漏感2\"结果]:成功":
                    //    this.lg2PassCount = (int.Parse(lg2PassCount) + 1).ToString();
                    //    break;
                    //case "[综测结果]:成功":
                    //    this.zcPassCount = (int.Parse(zcPassCount) + 1).ToString();
                    //    break;
                    //case "[综测结果]:失败":
                    //    this.zcFailCount = (int.Parse(zcFailCount) + 1).ToString();
                    //    break;
                    #endregion
                    case "[总体结果]失败":
                        this.SummaryFailCount = (int.Parse(SummaryFailCount) + 1).ToString();
                        break;
                    case "[总体结果]成功":
                        this.SummaryPassCount = (int.Parse(SummaryPassCount) + 1).ToString();
                        break;
                    #region 备注：之后处理 
                    #endregion
                }
                #region marked
                //ny1TotalCount = (int.Parse(ny1FailCount) + int.Parse(ny1PassCount)).ToString();
                //if (int.Parse(ny1TotalCount) > 0)
                //    ny1FailRate = (float.Parse(ny1FailCount) / float.Parse(ny1TotalCount)).ToString("P");
                //ny2TotalCount = (int.Parse(ny2FailCount) + int.Parse(ny2PassCount)).ToString();
                //if (int.Parse(ny2TotalCount) > 0)
                //    ny2FailRate = (float.Parse(ny2FailCount) / float.Parse(ny2TotalCount)).ToString("P");
                //ny3TotalCount = (int.Parse(ny3FailCount) + int.Parse(ny3PassCount)).ToString();
                //if (int.Parse(ny3TotalCount) > 0)
                //    ny3FailRate = (float.Parse(ny3FailCount) / float.Parse(ny3TotalCount)).ToString("P");
                //lg1TotalCount = (int.Parse(lg1FailCount) + int.Parse(lg1PassCount)).ToString();
                //if (int.Parse(lg1TotalCount) > 0)
                //    lg1FailRate = (float.Parse(lg1FailCount) / float.Parse(lg1TotalCount)).ToString("P");
                //lg2TotalCount = (int.Parse(lg2FailCount) + int.Parse(lg2PassCount)).ToString();
                //if (int.Parse(lg2TotalCount) > 0)
                //    lg2FailRate = (float.Parse(lg2FailCount) / float.Parse(lg2TotalCount)).ToString("P");
                //zcTotalCount = (int.Parse(zcFailCount) + int.Parse(zcPassCount)).ToString();
                //if (int.Parse(zcTotalCount) > 0)
                //    zcFailRate = (float.Parse(zcFailCount) / float.Parse(zcTotalCount)).ToString("P"); 
                #endregion
                SummaryTotalQty = (int.Parse(SummaryPassCount) + int.Parse(SummaryFailCount)).ToString();
                if (int.Parse(SummaryTotalQty) > 0)
                {
                    SummaryFailRate = (float.Parse(SummaryFailCount) / float.Parse(SummaryTotalQty)).ToString("P");
                    SummaryPassRate = (float.Parse(SummaryPassCount) / float.Parse(SummaryTotalQty)).ToString("P");
                }
            }
            #region marked
            #endregion
            if (msg.Contains("[停止模式]"))
            {
                Finishing();
            }
            else if (msg.Contains("[正常模式]"))
            {
                pbState.Image = (Image)htStateImage["RUN"];
            }
        }

        private void navigationPanePanel1_TabIndexChanged(object sender, EventArgs e)
        {

        }

        private void navigationPane1_TitlePanel_TabIndexChanged(object sender, EventArgs e)
        {
            if (navigationPane1.TabIndex == 2)//切换到io卡监控
            {
                tmr.Start();
            }
            else
            {
                tmr.Stop();
            }
        }

        private void navigationPane1_TabStopChanged(object sender, EventArgs e)
        {

        }

        private void navigationPane1_TabIndexChanged(object sender, EventArgs e)
        {
            if (navigationPane1.TabIndex == 2)//切换到io卡监控
            {
                tmr.Start();
            }
            else
            {
                tmr.Stop();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //this.listBox1.Items.Clear();
            //_cellLIne.Step();
            this.StartTestProc();
        }

        private void dataGridViewX1_Paint(object sender, PaintEventArgs e)
        {
        }

        private void dataGridViewX1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
        }

        private void Changedgvx1Corlor()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string value = row.Cells["测试结果"].Value.ToString();
                if (value.ToUpper().Contains("PASS"))
                {
                    row.Cells["测试结果"].Style.BackColor = Color.LightGreen;
                }
                else if (value.ToUpper().Contains("FAIL"))
                {
                    row.Cells["测试结果"].Style.BackColor = Color.Red;
                }
                else
                {
                    row.Cells["测试结果"].Style.BackColor = Color.Gray;
                }
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {

        }

        public void ResetState()
        {
            DialogResult dr = ShowQuestion("真的要清空数据,恢复到初始状态吗？");
            if (dr == DialogResult.Yes)
            {
                _cellLIne.ClearProducts();
                CountManager.ClearCounts();
                SummaryPassCount = "0";
                SummaryPassRate = "100%";
                SummaryFailCount = "0";
                SummaryFailRate = "0%";
                SummaryTotalQty = "0";
                dataGridView1.Rows.Clear();
                ShowMsg("清空操作被执行");
            }

        }

        private void dataGridViewX1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
         //   e.
        }
    }
}
